<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box {
            width: 400px;
            height: 100px;
            position: absolute;
            top: 50%;
            left: 50%;
            margin-left: -200px;
            margin-top: -50px;
            background-color: black;
            display: none;
        }
    </style>
</head>

<body>
    <button id="btn">按我弹出</button>
    <div id="box"></div>
    <script>
        var oBtn = document.getElementById('btn');
        var oBox = document.getElementById('box');
        // 点击按钮显示
        oBtn.onclick = function(e) {
                // 阻止事件继续传播到document
                e.stopPropagation();
                oBox.style.display = 'block';
            }
            // 点击页面任何部分的时候，弹出层关闭
        document.onclick = function() {
            oBox.style.display = 'none'
        }
        oBox.onclick = function(e) {
            // 阻止事件继续传播到document
            e.stopPropagation();
        }
    </script>
</body>

</html>